<?php

class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        header("Content-type: text/html; charset=utf-8");
        ini_set('session.gc_maxlifetime', 288000);
        $this->load->model('users_model');
        //$this->load->library('Fraud');//同盾科技
        //$this->load->library('Oauth');//qq第三方登录
        //$this->load->library('Weibo');//新浪微博第三方登录
        //$this->load->library('Crypt');

        $this->remote_url = array(
            "sms_url" => "http://api.tan.com/index.php?r=sms/send",
            "query_url" => "http://api.tan.com/index.php?r=plat/query",
            "recieve_url" => "http://api.tan.com/index.php?r=user/recieve",
        );
        $this->img_url = "http://www.tan.com";
        //网站全局配置
        $this->settings->load('config');
        $this->web_config = setting('config');

        //加载信用卡类别
        $this->settings->load('credit');
        $this->credit = setting('credit');
        //加载平台背景类别
        $this->settings->load('bg_type');
        $this->bgType = setting("bg_type");
        //加载任务配置
        $this->settings->load('task');
        $this->config_task = setting('task');
        //加载奖励配置
        $this->settings->load('award');
        $this->config_award = setting('award');
        //加载会员等级配置
        $this->settings->load('vip');
        $this->config_vip = setting('vip');

        //URL来路
        $HTTP_REFERER = @$_SERVER['HTTP_REFERER'];
        $this->redirectLink = $HTTP_REFERER ? $HTTP_REFERER : site_url();
        //判断登录状态
        $this->userinfo = $this->session->userdata('userinfo');
        //var_dump($this->session);exit;
        if ($this->userinfo) {
            $this->assign('uinfo', $this->userinfo);
        }
        if ($this->userinfo) {
            //获取用户信息
            $user_info = $this->users_model->get(intval($this->userinfo['user_id']));
            $user_info['head_img'] = strstr($user_info['head_img'], "http://") ? $user_info['head_img'] : "http://www.tan66.com" . $user_info['head_img'];

            $user_info['balance_zhu'] = number_format(intval($user_info['balance'] / 10000));
            $user_info['balance_format'] = number_format($user_info['balance']);

            $sms_count = $this->users_model->get_no_read_sms($this->userinfo['user_id']);
            $notice_count = $this->users_model->get_no_read_notice($this->userinfo['user_id']);
            $this->assign("sms_flag", $sms_count || $notice_count['count']);
            $bbs_account = $this->db->query("select * from tan_bbs_account where user_id = " . $this->userinfo['user_id'] . " limit 1")->row_array();
            if ($bbs_account) {
                $user_info['bbs_account'] = $bbs_account;
            }
        }
        $this->assign("alogin", $this->userinfo ? json_encode(array("uuid" => encrypt_user_id($this->userinfo['user_id']), "unick" => $this->userinfo['nickname'], "isline" => 1, "uname" => $this->userinfo['username'])) : json_encode(array("uuid" => 0, "unick" => "", "isline" => 0, "uname" => "")));

        $this->user_info = $user_info ? $user_info : '';
        //当前模块
        $this->module = $this->uri->segment(1) ? $this->uri->segment(1) : 'index';
        //当前页面
        $this->action = $this->uri->segment(2) ? $this->uri->segment(2) : 'index';
        $this->session_id = session_id();
        $this->assign('user_info', $this->user_info);
        $this->assign('module', $this->module);
        $this->assign('action', $this->action);
        $this->assign('SESSIONID', $this->session_id);
        $this->assign("redirectUrl", "?redirectUrl=" . urlencode("http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], "?") ? "&autoLogin=true" : "?autoLogin=true")));
        if ($_REQUEST['autoLogin']) {
            $this->sso_login();
        }

        $this->footer_data();

        if ($this->userinfo && !$_COOKIE['tan_uuid']) {
            $this->session->sess_destroy();
            header("Location:" . $this->redirectLink);
            exit;
        }
        $this->assign("style_dir", STYLE_DIR);//全局样式路径
    }

    public function assign($key, $val)
    {
        $this->cismarty->assign($key, $val);
    }

    public function display($html)
    {
        $this->cismarty->display($html);
    }

    function get_next_request_url()
    {
        $url = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
        return "?redirectUrl=" . urlencode($url);
    }

    function sso_login()
    {
        if (!$this->userinfo) {
            if ($_COOKIE['tan_uuid']) {
                $user_id = decrypt_user_id($_COOKIE['tan_uuid']);
                $uinfo = $this->users_model->get_user_by_uid($user_id);
                $info = array();
                $info['user_id'] = $uinfo['user_id'];
                $info['username'] = $uinfo['username'];
                $info['mobile'] = $uinfo['mobile'];
                $info['email'] = $uinfo['email'];
                $info['last_ip'] = $uinfo['last_ip'];
                $info['last_time'] = $uinfo['last_time'];
                $this->session->set_userdata('userinfo', $info);


                $this->userinfo = $this->session->userdata('userinfo');
                if ($this->userinfo) {
                    $this->assign('uinfo', $this->userinfo);
                }
                if ($this->userinfo) {
                    //获取用户信息
                    $user_info = $this->users_model->get(intval($this->userinfo['user_id']));
                    $user_info['head_img'] = strstr($user_info['head_img'], "http://") ? $user_info['head_img'] : "http://www.tan.com" . $user_info['head_img'];
                    $bbs_account = $this->db->query("select * from tan_bbs_account where user_id = " . $this->userinfo['user_id'] . " limit 1")->row_array();
                    if ($bbs_account) {
                        $user_info['bbs_account'] = $bbs_account;
                    }
                }

                $sms_count = $this->users_model->get_no_read_sms($this->userinfo['user_id']);
                $notice_count = $this->users_model->get_no_read_notice($this->userinfo['user_id']);
                $this->assign("sms_flag", $sms_count || $notice_count['count']);

                $this->user_info = $user_info ? $user_info : '';

                $this->assign("alogin", $this->userinfo ? json_encode(array("uuid" => encrypt_user_id($this->userinfo['user_id']), "unick" => $this->userinfo['nickname'], "isline" => 1, "uname" => $this->userinfo['username'])) : json_encode(array("uuid" => 0, "unick" => "", "isline" => 0, "uname" => "")));

                //当前模块
                $this->module = $this->uri->segment(1) ? $this->uri->segment(1) : 'index';
                //当前页面
                $this->action = $this->uri->segment(2) ? $this->uri->segment(2) : 'index';
                $this->session_id = session_id();
                $this->assign('user_info', $this->user_info);
                $this->assign('module', $this->module);
                $this->assign('action', $this->action);
                $this->assign('SESSIONID', $this->session_id);
            } else {
                if ($_REQUEST['redirectUrl']) {
                    www_redirect(www_base_url("account/login") . "?redirectUrl=" . $_REQUEST['redirectUrl']);
                } else {
                    www_redirect(www_base_url("account/login") . $this->get_next_request_url());
                }
            }
        }
    }

    //友情链接
    private function footer_data()
    {
        //合作机构
        $partners = $this->db->select('*')->from('partners')->where(array('is_show' => 1))->order_by("sort", "asc")->get()->result_array();
        $this->assign("partners", $partners);
        //总获取溜溜币
        $total_amount = $this->db->select('SUM(total) total_amount')->from('account')->get()->row_array();
        $total_amount = number_format($total_amount['total_amount']);
        $this->assign("total_amount", $total_amount);
        //总投资金额
        $total_invest_amount = $this->db->select('SUM(amount) total_amount')->from('plat_invest_list')->get()->row_array();
        $total_invest_amount = number_format(intval($total_invest_amount['total_amount'] + 244828480));
        $this->assign("total_invest_amount", $total_invest_amount);
        //总人数
        $total_users = $this->db->query('select count(*) total from tan_users order by user_id desc')->row_array();
        $total_users = $total_users['total'];
        $total_users = number_format($total_users - 2500);
        $this->assign("total_users", $total_users);

    }
}